package com.yiliao.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.yiliao.service.FinancialDetailsService;
import com.yiliao.util.DateUtils;
import com.yiliao.util.MessageUtil;

/**
 * 财务明细
 * @author Administrator
 *
 */
@Service("financialDetailsService")
public class FinancialDetailsServiceImpl extends ICommServiceImpl implements
		FinancialDetailsService {

	@Override
	public JSONObject getFinancialDetailsList(String beginTime, String endTime,
			int page) {
		try {
			
			//
			String cSql = "SELECT COUNT(t_id) total FROM t_financial_statements WHERE 1 =1 ";
			
			String qSql = "SELECT * FROM t_financial_statements WHERE 1 =1  ";
			
			if(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime)) {
				cSql = cSql + " AND t_statistical_date BETWEEN '"+beginTime+" 00:00:00' AND '"+endTime+" 23:59:59'";
				qSql = qSql + " AND t_statistical_date BETWEEN '"+beginTime+" 00:00:00' AND '"+endTime+" 23:59:59'";
			}
			qSql = qSql + " ORDER BY t_statistical_date DESC  LIMIT ?,10";
			
			//得到统计
			Map<String, Object> total = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(cSql);
			
			//得到数据
			List<Map<String,Object>> sqltoMap = this.getFinalDao().getIEntitySQLDAO().findBySQLTOMap(qSql, (page-1)*10);
			
			sqltoMap.forEach(s ->{
				try {
					s.put("t_statistical_date", DateUtils.format(DateUtils.parse(s.get("t_statistical_date").toString(), DateUtils.defaultDatePattern), DateUtils.defaultDatePattern));
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			});
			
			JSONObject json = new JSONObject();
			json.put("total", total.get("total"));
			json.put("rows", sqltoMap);
			return json;
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("获取列表异常!", e);
		}
		return null;
	}

	/*
	 * 统计收支(non-Javadoc)
	 * @see com.yiliao.service.FinancialDetailsService#getCollectPayTotal(java.lang.String, java.lang.String)
	 */
	@Override
	public MessageUtil getCollectPayTotal(String beginTime, String endTime) {
		
		MessageUtil mu = new MessageUtil();
		try {
			String reSql = "SELECT sum(t_recharge_money) AS totalMoney FROM t_recharge WHERE  t_order_state = 1";
			String putSql = "SELECT sum(t_money) AS totalMoney FROM t_put_forward WHERE t_order_state = 2 ";
			
			if(null !=beginTime && !beginTime.trim().isEmpty() && null!=endTime && !endTime.trim().isEmpty()){
				
				reSql = reSql+" AND t_fulfil_time  BETWEEN  '"+beginTime+" 00:00:00' and  '"+endTime+" 23:59:59'";
				putSql = putSql + "AND t_handle_time BETWEEN '"+beginTime+" 00:00:00' AND '"+endTime+" 23:59:59'";
			}
			
			Map<String, Object> map = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(reSql);
			
			Map<String, Object> putMap = this.getFinalDao().getIEntitySQLDAO().findBySQLUniqueResultToMap(putSql);
			
			map.put("putPay", putMap.get("totalMoney"));
			
			mu.setM_istatus(1);
			mu.setM_object(map);
			
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("统计收支异常!", e);
		}
		return mu;
	}

	
	
	
}
